var body = document.body;
function HandleClass(){
	//判断子弹是否打到敌人
	this.bulletFireEnemy = function(bulletControlle,enemyControlle,planeHero,energyBall,background){
//		console.log(energyBall);
		//拿出装子弹的数组
		//var bulletArray = bulletController.bulletArray;
		//拿出装载敌人的数组
		//enemyController.enemyArray;
		for (var i = 0; i < bulletControlle.bulletArray.length;i++) {
		for (var j= 0;j<enemyControlle.enemyArray.length;j++) {
//		for (var k = 0; k < energyBall.energyArray.length;k++) {
			//判断是否发生了碰撞
			var bulletX = bulletControlle.bulletArray[i].x;
			var bulletY = bulletControlle.bulletArray[i].y;
			var bulletWidth = bulletControlle.bulletArray[i].width;
			var bulletHeight = bulletControlle.bulletArray[i].height;
			
			var enemyX = enemyControlle.enemyArray[j].x;
			var enemyY = enemyControlle.enemyArray[j].y;
			var enemyWidth = enemyControlle.enemyArray[j].width;
			var enemyHeight = enemyControlle.enemyArray[j].height;
			
			//获取飞机的位置和大小
			var planeX = planeHero.planeX;
			var planeY = planeHero.planeY;
//			var energyx =energyBall.energyArray[k].x;
//			var energyy =energyBall.energyArray[k].y;
//			console.log(planeY);
			var planeWidth = 66;
			var planeHeight = 82;
//			var energywidth = 39;
//			var energyheight = 68;
//			console.log(planeX);
			
			if(planeX < enemyX + enemyWidth &&planeY< enemyY + enemyHeight&&planeX+planeWidth>enemyX&&planeY+planeHeight>enemyY){
				
				$("#restartScore").html("分数:"+ SUMSCORE);
				
				$("#messageOver").show();
				// 移除所有的敌人飞机
				bulletControlle.bulletArray = [];
				 enemyControlle.enemyArray = [];
				 energyBall.energyArray = [];
				 planeHero.init();
				 
				// 移除所有子弹
				context.clearRect(0,0,canvas.width,canvas.height);
				clearInterval(timer);
				SUMSCORE = 0;
				background.drawImage();
				
			}
			if(bulletX < enemyX + enemyWidth && bulletY < enemyY + enemyHeight && bulletX + bulletWidth > enemyX && bulletY + bulletHeight > enemyY){
				//移除子弹
				bulletControlle.bulletArray.splice(i,1);
				//碰撞了
				//1.敌人血量减一
				enemyControlle.enemyArray[j].blood -= 1;
				if(enemyControlle.enemyArray[j].blood <=0){
					var enemyAudio = enemyControlle.enemyArray[j].type+"_music";
					document.getElementById(enemyAudio).play();
					//消失
					//enemyController.enemyArray.splice(j,1);
					//此时isdie = true；
					enemyControlle.enemyArray[j].isDie = true;
				}
			}	
		}	
		}
	}
	


// 判断是否吃到了能力包
	this.planeIsEatEnergy = function(aPlane,energyBall){
		for (var i = 0; i < energyBall.energyArray.length;i++) {
			if (aPlane.planeX + PLANEWIDTH < energyBall.energyArray[i].x || aPlane.planeX > energyBall.energyArray[i].x + energyBall.energyArray[i].width || aPlane.planeY + PLANEHEIGHT < energyBall.energyArray[i].y || aPlane.planeY > energyBall.energyArray[i].y + energyBall.energyArray[i].height) {
				// 没有碰到
			}else{
				energyBall.energyArray[i].isGet = true;
				$("#bomb").show();
				aPlane.bulletInfo = {
					name:"bullet2",
					bulletWidth:48,
					bulletHeight:14
				}
				
				// 放大时间不超过15秒
				clearTimeout(fangdaTime);
				var fangdaTime = setTimeout(function(){
					aPlane.bulletInfo = {
					name:"bullet1",
					bulletWidth:6,
					bulletHeight:14
					}
					$("#bomb").hide();
					clearTimeout(fangdaTime);
						
				},15000);
				
			}
						
		}
		
}

	
	
	
	
	
	//操作飞机的方法
	this.handlePlane = function(aPlane){
		
		canvas.addEventListener("touchstart",function(event){
			var ev = event || window.event;
			// 取出手指的坐标(相对于 canvas 画布的坐标)
			var x = 	ev.touches[0].clientX - this.offsetLeft;
			var y = ev.touches[0].clientY - this.offsetTop;	
			var disX = x - aPlane.planeX;
			var disY = y - aPlane.planeY;
			// 必须点到飞机的时候才可以拖拽
			if (x>aPlane.planeX && x < (aPlane.planeX + PLANEWIDTH) && y > aPlane.planeY && y < (aPlane.planeY + PLANEHEIGHT)) {
				document.addEventListener("touchmove",handleMoveFun,false);
			}
		// 手指离开的时候 移除事件
		document.addEventListener("touchend",function(){
			document.removeEventListener("touchmove",handleMoveFun,false);
		},false);
			function handleMoveFun(event){
				var ev = event || window.event;
				//确定飞机的新坐标
				var newX = ev.touches[0].clientX - disX;
				var newY = ev.touches[0].clientY - disY;
				if (newX <= 0) {
					newX = 0;
				}
				if (newX >= canvas.width - PLANEWIDTH) {
					newX = canvas.width - PLANEWIDTH;
				}
				if (newY<= 0) {
					newY = 0;
				}
				if (newY >= canvas.height - PLANEHEIGHT) {
					newY = canvas.height - PLANEHEIGHT;
				}
				aPlane.planeX = newX;
				aPlane.planeY = newY;
			}
			
		},false);
		
		
		
		
		document.onkeydown = function(event){
			var ev = event || window.event;
			switch (ev.keyCode){
				//规定 0左 1右  2上  3下  -1代表飞机不动
				case 37://左
				aPlane.direction = 0;
					break;
				case 38://上
				aPlane.direction = 2;
					break;
				case 39://右
				aPlane.direction = 1;
					break;	
				case 40://下
				aPlane.direction = 3;
					break;					
			}
			aPlane.movePlane(5);
		}
		document.onkeyup = function(event){
			var ev = event || window.event;
			if(ev.keyCode == 37 || ev.keyCode == 38 || ev.keyCode == 39 || ev.keyCode == 39){
				aPlane.direction = -1;
			}
		}
	}
}



